package com.zhangc.blog.admin.web.controller.admin;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangc.blog.admin.entity.BlogCategoryEntity;
import com.zhangc.blog.admin.entity.BlogInfoEntity;
import com.zhangc.blog.admin.service.BlogCategoryServiceImpl;
import com.zhangc.blog.admin.service.BlogInfoServiceImpl;
import com.zhangc.blog.admin.web.dto.AjaxPutPage;
import com.zhangc.blog.admin.web.dto.AjaxResultPage;
import com.zhangc.blog.base.ResultBO;
import com.zhangc.blog.base.enums.DeleteStatusEnum;

@Controller
@RequestMapping("/admin")
public class CategoryController {

    @Autowired
    private BlogCategoryServiceImpl blogCategoryService;

    @Autowired
    private BlogInfoServiceImpl blogInfoService;

    /**
     * 分类的集合数据[用于下拉框]
     *
     * @param
     * @return com.site.blog.pojo.dto.Result<com.site.blog.entity.BlogCategory>
     * @date 2019/8/30 14:38
     */
    @ResponseBody
    @GetMapping("/v1/category/list")
    public ResultBO<List<BlogCategoryEntity>> categoryList() {
        QueryWrapper<BlogCategoryEntity> queryWrapper = new QueryWrapper<BlogCategoryEntity>();
        queryWrapper.lambda().eq(BlogCategoryEntity::getIsDeleted, DeleteStatusEnum.NO_DELETED.getStatus());
        List<BlogCategoryEntity> list = blogCategoryService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            ResultBO.error500(null);
        }
        return ResultBO.of(list);
    }

    @GetMapping("/v1/category")
    public String gotoBlogCategory() {
        return "adminLayui/category-list";
    }

    /**
     * 分类的分页
     *
     * @param ajaxPutPage
     * @param condition
     * @return com.site.blog.pojo.dto.AjaxResultPage<com.site.blog.entity.BlogCategory>
     * @date 2019/8/30 14:38
     */
    @ResponseBody
    @GetMapping("/v1/category/paging")
    public AjaxResultPage<BlogCategoryEntity> getCategoryList(AjaxPutPage<BlogCategoryEntity> ajaxPutPage, BlogCategoryEntity condition) {
        QueryWrapper<BlogCategoryEntity> queryWrapper = new QueryWrapper<>(condition);
        queryWrapper.lambda()
                .orderByAsc(BlogCategoryEntity::getCategoryRank)
                .ne(BlogCategoryEntity::getCategoryId, 1);
        Page<BlogCategoryEntity> page = ajaxPutPage.putPageToPage();
        blogCategoryService.page(page, queryWrapper);
        AjaxResultPage<BlogCategoryEntity> result = new AjaxResultPage<>();
        result.setData(page.getRecords());
        result.setCount(page.getTotal());
        return result;
    }

    /**
     * 修改分类信息
     *
     * @return com.site.blog.pojo.dto.Result<java.lang.String>
     * @author Linn-cn
     * @date 2020/9/1
     */
    @ResponseBody
    @PostMapping("/v1/category/update")
    public ResultBO<String> updateCategory(BlogCategoryEntity blogCategory) {
        BlogCategoryEntity sqlCategory = blogCategoryService.getById(blogCategory.getCategoryId());
        boolean flag = sqlCategory.getCategoryName().equals(blogCategory.getCategoryName());
        if (!flag) {
            BlogInfoEntity blogInfo = new BlogInfoEntity()
                    .setBlogCategoryId(blogCategory.getCategoryId())
                    .setBlogCategoryName(blogCategory.getCategoryName());
            UpdateWrapper<BlogInfoEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(BlogInfoEntity::getBlogCategoryId, blogCategory.getCategoryId());
            blogInfoService.update(blogInfo, updateWrapper);
            blogCategoryService.updateById(blogCategory);
        }
        return ResultBO.success();
    }

    /**
     * 修改分类状态
     *
     * @param blogCategory
     * @return com.site.blog.pojo.dto.Result
     * @date 2019/8/30 14:55
     */
    @ResponseBody
    @PostMapping("/v1/category/isDel")
    public ResultBO<String> updateCategoryStatus(BlogCategoryEntity blogCategory) {
        boolean flag = blogCategoryService.updateById(blogCategory);
        if (flag) {
            return ResultBO.success();
        }
        return ResultBO.error500(null);
    }

    /**
     * 清除分类信息
     *
     * @param blogCategory
     * @return com.site.blog.pojo.dto.Result
     * @date 2019/9/1 15:48
     */
    @ResponseBody
    @PostMapping("/v1/category/clear")
    public ResultBO<String> clearCategory(BlogCategoryEntity blogCategory) {
        if (blogCategoryService.clearCategory(blogCategory)) {
            return ResultBO.success();
        }
        return ResultBO.error500(null);
    }

    @GetMapping("/v1/category/add")
    public String addBlogConfig() {
        return "adminLayui/category-add";
    }

    /**
     * 新增分类信息
     *
     * @param blogCategory
     * @return com.site.blog.pojo.dto.Result
     * @date 2019/9/1 15:48
     */
    @ResponseBody
    @PostMapping("/v1/category/add")
    public ResultBO<String> addCategory(BlogCategoryEntity blogCategory) {
        blogCategory.setCreateTime(new Date());
        boolean flag = blogCategoryService.save(blogCategory);
        if (flag) {
            return ResultBO.success();
        }
        return ResultBO.error500(null);
    }

}
